package com.tyc.flink;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

import java.util.Arrays;
/**
 * Connect: DataStream,DataStream → ConnectedStream
 * CoMap, CoFlatMap: ConnectedStream → DataStream
 * @author tyc
 * @version 1.0
 * @date 2022-11-16 18:01:43
 */
public class Transform_6_Connect{
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> dataStreamSource1 = env.fromCollection(
                Arrays.asList("hello flink",
                        "hello java",
                        "hi program",
                        "hello",
                        "java"));

        DataStreamSource<Integer> dataStreamSource2 = env.fromCollection(
                Arrays.asList(1,2,3));

        ConnectedStreams<String, Integer> connect = dataStreamSource1.connect(dataStreamSource2);
        SingleOutputStreamOperator<Tuple2<String, Integer>> map = connect.map(new CoMapFunction<String, Integer, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map1(String value) throws Exception {
                //设置默认值
                return new Tuple2<>(value, 0);
            }

            @Override
            public Tuple2<String, Integer> map2(Integer value) throws Exception {
                //设置默认值
                return new Tuple2<>("word", value);
            }
        });

        map.print();

        env.execute();
    }
}
